﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.Windows.Navigation;
using System.Text;
using Microsoft.Phone.Tasks;

namespace SmartHYU_TestVersion.Life
{
    public partial class DepartmentInfoDetail : PhoneApplicationPage
    {
        private DepartInformationManager hyuDepartmentInfo = new DepartInformationManager();
        private List<Department> hyuDepartments = new List<Department>();

        // information of seleted item
        private string seletedDepartment;
        private string phoneNumberOfSeletedDepartment;
        private string uriOfSeletedDepartment;
        

        public DepartmentInfoDetail()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string selectedDepartmentCategoryIndexString;
            int selectedDepartmentCategoryIndex;

            if (NavigationContext.QueryString.TryGetValue("index", out selectedDepartmentCategoryIndexString))
            {
                // MessageBox.Show(selectedDepartmentCategoryIndexString);

                selectedDepartmentCategoryIndex = int.Parse(selectedDepartmentCategoryIndexString);

                // Show subtitle
                SubTitle.Text =
                    hyuDepartmentInfo.DepartmentCategories.Skip(selectedDepartmentCategoryIndex).First().Name;

                // Siplify object
                hyuDepartments = 
                    hyuDepartmentInfo.DepartmentCategories.Skip(selectedDepartmentCategoryIndex).First().Departments;

                // Display
                departmentInfoDetailListBox.ItemsSource = hyuDepartments;
            }
            else /* No query string : then go back */
            {
                NavigationService.GoBack();
            }

            
        }

        private void departmentInfoDetailListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectedIndex = departmentInfoDetailListBox.SelectedIndex;
            string uriInformation;
            StringBuilder contactInfo = new StringBuilder();

            // get datas of seleted department
            seletedDepartment = hyuDepartments.Skip(selectedIndex).First().Name;
            phoneNumberOfSeletedDepartment = "+82-2-2220-" + hyuDepartments.Skip(selectedIndex).First().PhoneNumber;
            uriOfSeletedDepartment = hyuDepartments.Skip(selectedIndex).First().Url;

            // set uri information
            if (uriOfSeletedDepartment == null)
            {
                uriInformation = "No Homepage";
            }
            else
            {
                uriInformation = uriOfSeletedDepartment;
            }

            // assemble contact information and display to screen
            contactInfo.Append(seletedDepartment);
            contactInfo.Append(" / ");
            contactInfo.Append(phoneNumberOfSeletedDepartment);
            contactInfo.Append(" / ");
            contactInfo.Append(uriInformation);

            departmentContactInfoTextBlock.Text = contactInfo.ToString();
        }

        /***************************************
         *          Button behaviors
         ***************************************/
        private void callButton_Click(object sender, RoutedEventArgs e)
        {
            PhoneCallTask callDepartment = new PhoneCallTask();
            callDepartment.DisplayName = seletedDepartment;
            callDepartment.PhoneNumber = phoneNumberOfSeletedDepartment;

            callDepartment.Show();
        }

        private void homepageButton_Click(object sender, RoutedEventArgs e)
        {
            if (uriOfSeletedDepartment != null)
            {
                WebBrowserTask homepageOfDepartment = new WebBrowserTask();
                homepageOfDepartment.URL = uriOfSeletedDepartment;
                
                homepageOfDepartment.Show();
            }
            else /* If data has no uri information */
            {
                MessageBox.Show("Cannot find homepage information of the department");
            }

        }
    }
}